<?php if(!defined('FRAME')) exit('No direct script access allowed');
/**
 * 文件操作
 * @author wisoch@hotmail.com
 *
 */
class File {
	private $filename;
	private $fp;
	public function __construct($filename) {
		if(file_exists($filename)) $this->filename = $filename;		
	}
	public function __destruct() {
		if($this->fp) fclose($this->fp);
	}
	/**
	 * 文件末尾添加内容
	 * @param unknown_type $content
	 * @param unknown_type $line
	 */
	public function append($content, $line = true) {
		$content = $this->_serialize($content);
		if(!$this->filename) return false;
		$this->fp = fopen($this->filename, "ab");
		if($line) $content = "\r\n".trim($content);
		if(fwrite($this->fp, $content)) $return = true;
		else $return = false;
		fclose($this->fp);
		return $return;
	}
	/**
	 * 获取文件内容
	 * @param unknown_type $line
	 */
	public function body($line = true) {
		if(!$this->filename) return false;
		if(!$line) return file_get_contents($this->filename);
		else return file($this->filename);
	}
	/**
	 * 获取文件最后修改时间
	 */
	public function lastModified() {
		$this->fp = fopen($this->filename, "rb");
        return @filemtime($this->fp);
    }
	private function _serialize($value) {
		if(is_string($value)) return $value;
		else return serialize($value);
	}
}
